/**
 * @author Mirko Bordjoski
 */	
package org.piwik.logic
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.managers.CursorManager;	
	
	/**
	* [internal-use] - The Caller class loads the data from Piwik. 
	*/
	public class Caller
	{
		/**
		 * @private 
	 	*/
		private var loader:URLLoader;
		
		/**
		 * @private 
	 	*/
		private var _url:String;
		
		/**
		 * @private 
	 	*/
		private var _who:String;
		
		/**
		 * @private 
	 	*/
		private var weak:Boolean = false;
		
		
		
		/**
		 * @private 
	 	*/
		public var onLoad:Function;
		
		/**
		 * @private 
	 	*/
		public var onIOError:Function;
		
		/**
		 * @private 
	 	*/
		public var onSecurityError:Function;
		
		/**
		 * @private 
	 	*/
		public var showBusyCursor:Boolean;
		
		
		
		
		
		/**
		* Constructor.
		* <p />
		* The Caller class loads the data from Piwik.
		* <p /> 
		* 
		* @param	url					URL to load.
		* @param	$who				[optional] - Request type of the call -  defined by PiwikRequest.
		* @param	$onLoad				[optional] - Listener.
		* @param	$onIOError			[optional] - Listener.
		* @param	$onSecurityError	[optional] - Listener.
		* @param	$autoLoad			[optional] - Can be <code>true</code> or <code>false</code>. Default is <code>true</code>.
		* @param	$weak				[optional] - If this value is <code>true</code>, URLLoader will be destroid after loading.
		* 
		*/	
		public function Caller(
								url:String, 
								$who:String = "", 
								$onLoad:Function = null, 
								$onIOError:Function = null, 
								$onSecurityError:Function = null, 
								$autoLoad:Boolean = true, 
								$showBusyCursor:Boolean = true, 
								$weak:Boolean = false
								)
		{
			onLoad = $onLoad;
			onIOError = $onIOError;
			onSecurityError = $onSecurityError;
			showBusyCursor = $showBusyCursor;
			weak = $weak;
			
			_url = url;
			_who = $who;
			
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, dataLoaded);
			loader.addEventListener(IOErrorEvent.IO_ERROR, IOError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityError);
			
			if($autoLoad)			
				load();				
		}
		
		
		
		/**
		* [internal-use] - Static function. Returns new Caller. 
		* <p/>
		* 
		* @param	$url				URL to load.
		* @param	$who				[optional] - Request type of the call -  defined by PiwikRequest.
		* @param	$onLoad				[optional] - Listener.
		* @param	$onIOError			[optional] - Listener.
		* @param	$onSecurityError	[optional] - Listener.
		* @param	$autoLoad			[optional] - Can be true or false. Default is true.
		* 
		*/	
		public static function makeCall(
										$url:String, 
										$who:String = "", 
										$onLoad:Function = null, 
										$onIOError:Function = null, 
										$onSecurityError:Function = null, 
										$autoLoad:Boolean = true, 
										$showBusyCursor:Boolean = true
										):Caller
		{			
			return new Caller($url, $who, $onLoad, $onIOError, $onSecurityError, $autoLoad, $showBusyCursor, true);
		}
		
		
		/**
		* [internal-use] Sending request to Piwik Analytics.
		*/
		public function load():void
		{
			loader.load(new URLRequest(_url));
			if(showBusyCursor != false){CursorManager.setBusyCursor();}
		}
		
		
		/**
		 * @private 
	 	*/
		private function dataLoaded(e:Event):void
		{
			if(showBusyCursor != false){CursorManager.removeBusyCursor();}
			if(onLoad != null) {onLoad(loader.data, _who);}
			if(weak != false){loader = null;}
		}	
		
		/**
		 * @private 
	 	*/	
		private function IOError(e:IOErrorEvent):void
		{
			if(showBusyCursor != false){CursorManager.removeBusyCursor();}
			if(onIOError != null) {onIOError(e.toString());}
			if(weak != false){loader = null;}			
		}
		
		/**
		 * @private 
	 	*/
		private function securityError(e:SecurityErrorEvent):void
		{
			if(showBusyCursor != false){CursorManager.removeBusyCursor();}
			if(onSecurityError != null) {onSecurityError(e.toString())};
			if(weak != false){loader = null;}			
		}
		
		

	}
}